<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@ include file="../../../../header.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>

</head>
<body>
	<div class="mod-toolbar-top cf" style="margin-top: 10px;">
		<div class="fr" style="margin-top: 30px;">
			<a href="javascript:void();" class="ui-btn ui-btn-sp mrb"
				id="btn-add">新增</a> 
		</div>
	</div>
	<div class="grid-wrap" align="left" style="margin-left: 10px;">
		<table id="grid1"></table>
	</div>
	<script type="text/javascript">
		function clearGrid() {
			$("#grid").jqGrid('clearGridData');
		}
		//添加图标
		function operationFormat(cellValue, options, rowObject) {
			return '<div class="operating" data-id="' + options.rowId + 
			'"><span class="ui-icon ui-icon-pencil" title="编辑" data-toggle="modal" data-target="#myModal"></span>&nbsp&nbsp'
					+ '<span class="ui-icon ui-icon-trash" title="删除"></span>';
		}
		$(function() {
			$("#grid1").jqGrid(
				{
					url : "categoryType/list",
					height : 400,
					autowidth: true,
					shrinkToFit : false,
					autoScroll : false,
					rownumbers : true,
					rowNum : -1,
					cellEdit : true,
					cellsubmit : "clientArray",
					colNames : [ "操作", "ID", "参数类别"],
					colModel : [ {
						name : "operating",
						width : 70,
						formatter : operationFormat,
						align : "center",
						sortable : false
					}, {
						name : "id",
						index : "id",
						key : true,
						editable : false,
						sorttype : "int",
						align : "center"
					}, {
						name : 'type',
						edittype : "text",
						align : "center",
						sortable : false,
						width : 200
					}],
					loadComplete : function() {
						loadover();
					},
					gridComplete : function() {
					},
					afterInsertRow : function() {
					},
					beforeEditCell : function(rowid, cellname, v, iRow,iCol) {
						lastrow = iRow;
						lastcell = iCol;
					}
				});
			initTableRow();
			initEvent();
		})
		//设置每行的高度
		function loadover() {
			var grid = $("#grid1");
			var ids = grid.getDataIDs();
			for (var i = 0; i < ids.length; i++) {
				grid.setRowData(ids[i], false, {
					height : 35
				});
			}
		}
		function initTableRow() {
			for (var i = 1; i <= 4; i++) {
				jQuery("#grid1").jqGrid('addRowData', i, {});
			}
			loadover();
		}
		//添加事件
		function initEvent() {
			$("#btn-add").click(function(a) {
				handle.operate("add");
			}) , $("#grid1").on("click", ".operating .ui-icon-pencil", function(a) {
				var rowid = $(this).parent().data("id");
				var rowdata=$("#grid1").jqGrid("getRowData",rowid);
				handle.operate("edit",rowid,rowdata);
			}), $("#grid1").on("click", ".operating .ui-icon-trash", function(a) {
				a.preventDefault();
				var rowid = $(this).parent().data("id");
				handle.del(rowid);
			}),$("#btn-record").click(function(a) {
				$("#grid1").jqGrid().trigger("reloadGrid");
			}),$("#btn-setsession").click(function(a){
				$.ajax({
					url:encodeURI("user/setsession"),
					type:"POST",
					success:function(data){
						alert(data);
					}
				});	
			}),$("#btn-getsession").click(function(a){
				$.ajax({
					url:encodeURI("user/getsession"),
					type:"POST",
					success:function(data){
						alert(data);
					}
				});	
			})
		}
		handle = {
				operate: function(oper, rowid, rowdata) {
					var title;
					var data={};
					if ("add" == oper) {
						title = "新增参数类别";
						data={
							operate: oper,
							callback: this.callback
						}
					} else {
						title = "修改参数类别";
						data={
							operate:oper,
							rowdata:rowdata,
							rowid:rowid,
							callback:this.callback
						}
					}
					this.dialog = $.dialog({
						title: title,
						data:data,
						content:"url:categoryType/editjsp",
						width: 500,
						height: 250,
						max: false,
						min: false,
						cache: false,
						lock: true
					});
				},
				del: function(rowId) {
					$.dialog.confirm("删除的参数类型将不能恢复，请确认是否删除？", function() {
						/* Public.ajaxPost("category/deleteType", {
							id: rowId
						}, function(data) {
							alert(data);
							$("#grid1").jqGrid().trigger("reloadGrid");
						}); */
						$.ajax({
							url:encodeURI("categoryType/delete"),
							type:"POST",
							data:{
								"id":rowId
							},
							success:function(data){
								$("#grid1").jqGrid().trigger("reloadGrid");	
							}
						});
					});
				},
				callback: function() {
					$("#grid1").jqGrid().trigger("reloadGrid");
				}
			};
	</script>
</body>
</html>